/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.sigawe.gateway.core;

import net.sigawe.gateway.Properties.Loader;
import net.sigawe.gateway.xml.XMLFactory;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FromContainsFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
/**
 *
 * @author Rahman
 */
public class Receiver implements MessageListener {

    XMPPConnection connection;
    XMLFactory xMLFactory = new XMLFactory();
    String HOST_SERVER = null;
    Loader loader = new Loader();
    public Receiver(XMPPConnection connection) {
        this.connection = connection;
        HOST_SERVER = loader.getFile().getProperty("SERVER_NAME");
        PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class), new FromContainsFilter(HOST_SERVER));
        PacketCollector myCollector = connection.createPacketCollector(filter);
        PacketListener myListener = new PacketListener() {
            public void processPacket(Packet paket) {
                //Proses untuk format XML dan menentukan Layanan
                xMLFactory.filterMessage(paket.toXML());
            }
        };
// Register the listener.
        connection.addPacketListener(myListener, filter);

    }

    public void processMessage(Chat arg0, Message arg1) {
        System.out.println("NEW MESSAGE");
    }
}
